const Koa = require('koa')
const app = new Koa()
const debug = require('debug')('koa-weapp-demo')
const response = require('./middlewares/response')
const bodyParser = require('koa-bodyparser')
const config = require('./config')

// 使用响应处理中间件
app.use(response)

// 解析请求体
app.use(bodyParser())

// 引入路由分发
const router = require('./routes')
app.use(router.routes())

// 启动程序，监听端口
app.listen(config.port, () => debug(`listening on port ${config.port}`))

  wx.login({
    success: function (loginCode) {
      var appid = 'wx1dce471340bde172'; //填写微信小程序appid  
      var secret = '414e7a9e501ceb729cc2114785085dda'; //填写微信小程序secret  

      //调用request请求api转换登录凭证  
      wx.request({
        url: 'https://api.weixin.qq.com/sns/jscode2session?appid=‘+<code></code>appid+’&secret=‘+secret+’&grant_type=authorization_code&js_code=' + loginCode.code,
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res.data.openid) //获取openid  
        }
      })
    }
  })

